# coding=utf-8

from data_structure import tools as tool

def get_stack_value(input_str):
    # 存放操作数
    operator = []
    # 存放操作符
    operate = []
    # 存放顶部操作符
    top = ''
    # 存放flag，flag为真，不添加当前字符
    flag = False
    # 临时操作数
    temp_str = ''
    # 预处理：basic_work(input_str)
    input_str = tool.basic_work(input_str)
    for index in range(len(input_str)):
        # 遇到操作符，进行下面操作
        if len(operate) >= 1:
            top = operate[len(operate) - 1]
        else:
            top = ''
        # 测试专用一：打印每次操作数和操作符
        # print('top',operator)
        # print('top',operate)
        if input_str[index] == '+':
            flag,temp_str = tool.set_operator_value(temp_str,operator)
            tool.simple_caculate('+', top, 'out', input_str[index], operate, operator)
        if input_str[index] == '-':
            flag, temp_str = tool.set_operator_value(temp_str, operator)
            tool.simple_caculate('-', top, 'out', input_str[index], operate, operator)
        if input_str[index] == '*':
            flag, temp_str = tool.set_operator_value(temp_str, operator)
            tool.simple_caculate('*', top, 'out', input_str[index], operate, operator)
        if input_str[index] == '/':
            flag, temp_str = tool.set_operator_value(temp_str, operator)
            tool.simple_caculate('/', top, 'out', input_str[index], operate, operator)
        if input_str[index] == '%':
            flag, temp_str = tool.set_operator_value(temp_str, operator)
            tool.simple_caculate('%', top, 'out', input_str[index], operate, operator)
        if input_str[index] == '^':
            flag, temp_str = tool.set_operator_value(temp_str, operator)
            tool.simple_caculate('^', top, 'out', input_str[index], operate, operator)
        if input_str[index] == '&':
            flag, temp_str = tool.set_operator_value(temp_str, operator)
            tool.simple_caculate('&', top, 'out', input_str[index], operate, operator)
        if input_str[index] == '.':
            flag = False
        if input_str[index] == '(':
            operate.append(input_str[index])
            flag = True
        if input_str[index] == ')':
            flag, temp_str = tool.set_operator_value(temp_str, operator)
            if tool.compare_operation(')', top, 'out') == '<':
                if len(operator) >= 2:
                    while operate[len(operate) - 1] == '(':
                        operate.pop()
                    operator.append(tool.compute_result(operator.pop(), operator.pop(), operate.pop()))
                # 测试专用二：调试带括号的问题
                # print(operator)
                # print(operate)
                if len(operate) >= 1 and operate[len(operate) - 1] == '(':
                    operate.pop()
                else:
                    if len(operator) >= 2 :
                        operator.append(tool.compute_result(operator.pop(), operator.pop(), operate.pop()))
                        operate.pop()
        # 遇到数字非法，直接退出;否则，组成临时字符
        if (input_str[index] < '0' or input_str[index] > '9') and tool.not_operate(input_str[index]):
            input_str = 'error in the\t'+str(index+1) + '\tcharacter!\t' + input_str + '->' + input_str[index]
            return input_str
        elif input_str[index] == ' ':
            continue
        else:
            if flag:
                flag = False
            else:
                temp_str += input_str[index]
        # 遇到‘=’且为最后一个字符，返回结果
        if input_str[index] == '=' and index == len(input_str)-1:
            length = len(operate)
            if length >= 1:
                operate.pop()
            else:
                pass
            while len(operate) > 0:
                if len(operator) >= 2:
                    operator.append(tool.compute_result(operator.pop(), operator.pop(), operate.pop()))
                else:
                    operate.pop()
    return str(operator)

